function init() {
    document.addEventListener("deviceready", deviceReady, true);
    delete init;
}

function checkAuthentication() {
    if (localStorage.profile_id > 0) {
        wd_redirect('home.html');
    }
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
            //            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
            //            $('#'+id_append+'').show();
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function checkValidate(id_append) {
    var emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,6}$/i,
            numericRegex = /^[0-9-]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check


    if ($('input[name="email"]').val() == '' || $('input[name="email"]').val() == 'E-mail') {
        msgEr = errormsg[2].replace('%s', 'email');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="email"]'), num_milliseconds);
        return false;
    } else if ($('input[name="email"]').val() != '') {
        valueStr = $('input[name="email"]').val();
        if (!emailRegex.test(valueStr)) {
            msgEr = errormsg[4].replace('%s', 'email');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="email"]'), num_milliseconds);
            return false;
        } 
    } 



    return true;
}
function handleLogin() {
    if (checkValidate('error-msg')) {
        var form = $("#login");
        //disable the button so we can't resubmit while we wait
        $("#submit", form).attr("disabled", "disabled");
        var u = $("#login-email", form).val();

        if (u != '') {
            $.post(localStorage.domain_app+"/webservice/forgotPassword.php", {
                userEmail: u

            }, function(res) {
                if (res.error == 0) {
                   
                    navigator.notification.alert(res.msg, function() {
                    });
                    wd_redirect('login.html');
                } else {
                    var num_milliseconds = 3000;
                    $('#error-msg').addClass('active').children().html(res.msg);
                    hideError_2('error-msg', num_milliseconds);
                    return false;
                }
                
                $("#submit").removeAttr("disabled");
            }, "json");
        } else {
            //Thanks Igor!
            navigator.notification.alert("You must enter a email", function() {
            });
            $("#submit").removeAttr("disabled");
        }
    }

    return false;
}

function eventClickEnter(e) {
    if (e.keyCode == 13) {
        handleLogin();
    }
}
$(function() {
    checkAuthentication();


    $("#submit").click(function() {
        handleLogin();
    });



    $('input[name="email"]').focus(function() {
        $(this).css("color", "#ffffff");
        if (this.value == "E-mail") {
            this.value = '';
        }
        if (this.value != "E-mail") {
            this.select();
        }
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
})